package com.xiaoq.distribution.endpoint;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.xiaoq.shop.entity.SimpleLock;
import com.xiaoq.shop.service.ISimpleLockService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.RedisTemplate4JedisCluster;
import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.ConstantUtil;
import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.distribution.entity.AgreementEntity;
import com.xiaoq.distribution.entity.DistriTicketEntity;
import com.xiaoq.distribution.entity.TicketPrice;
import com.xiaoq.distribution.service.IAgreementService;
import com.xiaoq.distribution.service.IDistriTicketService;
import com.xiaoq.distribution.vo.DistriTicketVo;
import com.xiaoq.vo.PagingVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@RestController
@RequestMapping(value = "/api/v1/distriticket")
@Api(tags={"门票分销相关Api"})
public class DistriTicketEndpoint {
	
	@Autowired
	private IAgreementService agreementSer;
	
	@Autowired
	private IdGenerator idGenerator;
	
	@Autowired
	private IDistriTicketService distriTicketSer;
	
	@Autowired
	private RedisTemplate4JedisCluster redisTemplate;

	@Autowired
	private ISimpleLockService simpleLockService;
	
	private class DistriTicketGenericDataResponse extends GenericDataResponse<DistriTicketVo> {}
	
	private class DistriTicketListGenericDataResponse extends GenericListResponse<DistriTicketVo> {}
	
	private class AgreeMentImgGenericDataResponse extends GenericDataResponse<List> {}
	
	@ApiOperation(value = "新增分销门票", notes = "新增分销门票")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "新增分销门票") })
	@PostMapping(value = "", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> addDistriTicket(@RequestBody DistriTicketVo distriTicketVo,HttpServletRequest request) {
		
		String agreementId = distriTicketVo.getAgreementId();
		AgreementEntity agreement = agreementSer.findById(agreementId);
		if(agreement==null){
			return GenericResponse.ng("不存在对应的合同！");
		}
		DistriTicketEntity distriTicket = new DistriTicketEntity();
		BeanUtils.copyProperties(distriTicketVo, distriTicket);
		distriTicket.setId(idGenerator.nextId());
		distriTicket.setAgreementId(agreement.getId());
		distriTicket.setShopId(agreement.getShopId());
		distriTicket.setMerchantId(agreement.getMerchantId());
		distriTicket.setDistribId(agreement.getDistribId());
		distriTicket.setDistribName(agreement.getDistribName());
		distriTicket.setCommitAuthNum(0L);
		distriTicket.setStatus(ConstantUtil.DISTRITICKET_4_VERIFY);
		distriTicketSer.save(distriTicket);

		SimpleLock simpleLock = new SimpleLock();
		simpleLock.setId(idGenerator.nextId());
		simpleLock.setTargetId(distriTicket.getId());
		simpleLock.setLockStatus(0);
		simpleLockService.save(simpleLock);

		return GenericResponse.ok();
		
	}
	
	@ApiOperation(value = "查询门票详情", notes = "查询门票详情")
	@ApiResponses({ @ApiResponse(code = 200, response = DistriTicketGenericDataResponse.class, message = "查询门票详情") })
	@GetMapping(value = "/detail", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> findDistriTicket(@RequestParam(value="id")@ApiParam(value="门票Id")String id) {
		DistriTicketEntity distriTicket = null;
		distriTicket = distriTicketSer.findById(id);
		if(distriTicket==null){
			return GenericResponse.ng("查询的门票不存在！");
		}
		DistriTicketVo distriTicketVo = new DistriTicketVo();
		BeanUtils.copyProperties(distriTicket, distriTicketVo);
		return DistriTicketGenericDataResponse.okWithData(distriTicketVo);
		
	}
	
	@ApiOperation(value = "授权门票列表", notes = "授权门票列表")
	@ApiResponses({ @ApiResponse(code = 200, response = DistriTicketListGenericDataResponse.class, message = "授权门票列表") })
	@GetMapping(value = "", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> findDistributors(@ModelAttribute(value = "pvo") PagingVo pvo, @RequestParam(value="agreementId")@ApiParam(value="合同Id")String agreementId,HttpServletRequest request) {
		
		List<DistriTicketEntity> distriTickets = distriTicketSer.findDistriTickets(agreementId, pvo);
		List<DistriTicketVo> distriTicketVoList = new ArrayList<>();
		DistriTicketVo distriTicketVo = null;
		for(DistriTicketEntity distriTicket:distriTickets){
			distriTicketVo = new DistriTicketVo();
			BeanUtils.copyProperties(distriTicket, distriTicketVo);
			distriTicketVoList.add(distriTicketVo);
		}
		return DistriTicketListGenericDataResponse.listAndCount(distriTicketVoList, (Long)pvo.getTc());
		
	}
	
	@ApiOperation(value = "编辑授权门票", notes = "编辑授权门票")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "编辑授权门票") })
	@PutMapping(value = "", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> editdistriTicket(@RequestBody DistriTicketVo distriTicketVo,HttpServletRequest request) {
		String id = distriTicketVo.getId();
		DistriTicketEntity distriTicket = distriTicketSer.findById(id);
		String ticketId = distriTicket.getTicketId();
		if(StringUtils.isNotBlank(ticketId)){
			distriTicket.setTicketId(distriTicketVo.getTicketId());
			distriTicket.setTicketType(distriTicketVo.getTicketType());
			distriTicket.setTicketName(distriTicketVo.getTicketName());
		}
		Long prePrice =  distriTicketVo.getPrePrice();
		if(prePrice!=null){
			distriTicket.setPrePrice(prePrice);
		}
		
		Long authNum =  distriTicketVo.getAuthNum();
		if(authNum!=null){
			distriTicket.setAuthNum(authNum);
		}
		
		String reTicket =  distriTicketVo.getReTicket();
		if(StringUtils.isNotBlank(reTicket)){
			distriTicket.setReTicket(reTicket);
		}
		String balance =  distriTicketVo.getBalance();
		if(StringUtils.isNotBlank(balance)){
			distriTicket.setBalance(balance);
		}
		Long distriTicketStart =  distriTicketVo.getDistriTicketStart();
		if(distriTicketStart!=null){
			distriTicket.setDistriTicketStart(distriTicketStart);
		}
		Long distriTicketEnd = distriTicketVo.getDistriTicketEnd();
		if(distriTicketEnd!=null){
			distriTicket.setDistriTicketEnd(distriTicketEnd);
		}
		List<TicketPrice> tickaetPrices = distriTicketVo.getTicketPrices();
		if(tickaetPrices!=null&&tickaetPrices.size()!=0){
			distriTicket.setTicketPrices(tickaetPrices);
		}
		distriTicketSer.save(distriTicket);
		return GenericResponse.ok();
		
	}
	
	@ApiOperation(value = "取消授权", notes = "取消授权")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "取消授权") })
	@PutMapping(value = "/cancelauth", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> cancelVarify(@RequestParam(value="id")@ApiParam(value="门票Id")String id) {
		DistriTicketEntity distriTicket = distriTicketSer.findById(id);
		distriTicket.setStatus(ConstantUtil.DISTRITICKET_CANCEL_VERIFY);
		distriTicketSer.save(distriTicket);
		return GenericResponse.ok();		
	}
	
	@ApiOperation(value = "审核门票列表", notes = "审核门票列表")
	@ApiResponses({ @ApiResponse(code = 200, response = DistriTicketListGenericDataResponse.class, message = "审核门票列表") })
	@GetMapping(value = "/authTicket", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> findVerifyDistributors(@ModelAttribute(value = "pvo") PagingVo pvo, HttpServletRequest request) {
		String userId = (String) request.getAttribute("userId");
		String shopId = redisTemplate.get(ConstantUtil.REDIS_PREFIX_MANAGER_SHOP_RELATION, userId);
		List<DistriTicketEntity> distriTickets = distriTicketSer.findDistriTicketsByStatus(shopId, pvo, ConstantUtil.DISTRITICKET_4_VERIFY);
		List<DistriTicketVo> distriTicketVoList = new ArrayList<>();
		DistriTicketVo distriTicketVo = null;
		for(DistriTicketEntity distriTicket:distriTickets){
			distriTicketVo = new DistriTicketVo();
			BeanUtils.copyProperties(distriTicket, distriTicketVo);
			distriTicketVoList.add(distriTicketVo);
		}
		return DistriTicketListGenericDataResponse.listAndCount(distriTicketVoList, (Long)pvo.getTc());
		
	}
	
	@ApiOperation(value = "提交审核", notes = "提交审核")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "提交审核") })
	@PutMapping(value = "/forAuth", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> forAuth(@RequestParam(value="id")@ApiParam(value="门票Id")String id) {
		DistriTicketEntity distriTicket = distriTicketSer.findById(id);
		distriTicket.setStatus(ConstantUtil.DISTRITICKET_4_VERIFY);
		distriTicketSer.save(distriTicket);
		return GenericResponse.ok();		
	}
	
	@ApiOperation(value = "同意授权接口", notes = "同意授权接口")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "同意授权接口") })
	@PutMapping(value = "/agreeauth", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> agreeVarify(@RequestParam(value="id")@ApiParam(value="门票Id")String id) {
		DistriTicketEntity distriTicket = distriTicketSer.findById(id);
		distriTicket.setStatus(ConstantUtil.DISTRITICKET_VERIFY_SUCCESS);
		distriTicketSer.save(distriTicket);
		return GenericResponse.ok();		
	}
	
	@ApiOperation(value = "驳回授权接口", notes = "驳回授权接口")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "驳回授权接口") })
	@PutMapping(value = "/refuseauth", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> refuseVarify(@RequestParam(value="id")@ApiParam(value="门票Id")String id) {
		DistriTicketEntity distriTicket = distriTicketSer.findById(id);
		distriTicket.setStatus(ConstantUtil.DISTRITICKET_VERIFY_FAIL);
		distriTicketSer.save(distriTicket);
		return GenericResponse.ok();		
	}
	
	@ApiOperation(value = "查询门票合同扫描件", notes = "查询门票合同扫描件")
	@ApiResponses({ @ApiResponse(code = 200, response = AgreeMentImgGenericDataResponse.class, message = "查询门票合同扫描件") })
	@GetMapping(value = "/4agreeImg", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> findAgreementByTicketId(@RequestParam(value="id")@ApiParam(value="门票Id")String id) {
		DistriTicketEntity distriTicket = distriTicketSer.findById(id);
		String agreementId = distriTicket.getAgreementId();
		AgreementEntity agreement = agreementSer.findById(agreementId);
		List<String> agreeImgUrls = agreement.getAgreeImgUrls();
		return AgreeMentImgGenericDataResponse.okWithData(agreeImgUrls);
		
	}

}
